
require("config")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")

local GameScene  = class("GameScene", function()
    return display.newScene("GameScene")
end)


local TAG_MYSCENE      = 10

function GameScene:ctor()

  local item = ui.newImageMenuItem({image='CloseNormal.png',imageSelected='CloseSelected.png',listener=onClicked})
  local menu = ui.newMenu({item}):addTo(self)
  menu:pos(display.width-40,display.height-40)
  menu:setZOrder(4)
  local touch = display.newLayer():addTo(self)
  --touch:setTouchEnabled(true)
  touch:addNodeEventListener(cc.NODE_TOUCH_EVENT, onTouch)

  --local myScene = display.newNode():addTo(self)
  --myScene:setZOrder(3)
  local myScene = TouchGroup:create():addTo(self)                       --widget必需用TuochGrup(UILayer)做为父节点，用addWidget加入
  myScene:setTag(TAG_MYSCENE)
  local Reader = GUIReader:shareReader()
  local myUIScene = Reader:widgetFromJsonFile('myLayer01.ExportJson')   --:addTo(myScene)
  myScene:addWidget(myUIScene)
  printf('myUIScene tag = %d',myUIScene:getTag()) 
  local rootScene = myUIScene:getChildByTag(100)
  if not rootScene then CCLuaLog('Not have tag 100 children')
  else CCLuaLog('have tag 1 children rootScene') end

  --[[
  local myBackground = UIHelper:seekWidgetByTag(myUIScene, 100)                           --图片是UIImageView类
  if not myBackground then CCLuaLog('Not have myBackground children')
  else CCLuaLog('have children myBackground') end
  --myBackground:loadTexture('Hello_cocos.png')                                             --变更图片
  myBackground:removeSelf()
  ]]

  local myBT01 = UIHelper:seekWidgetByTag(myUIScene, 101) 
  myBT01:addTouchEventListener(function(tager,event) 
    --CCLuaLog('onTOUCH')
    if event == TOUCH_EVENT_BEGAN then 
      CCLuaLog(type(touch))
      CCLuaLog(type(event))
      CCLuaLog('onTOUCH began')
      printf('tager name = %s',tager:getName())
    elseif event == TOUCH_EVENT_MOVED then
      CCLuaLog('onTOUCH moved')
    elseif event == TOUCH_EVENT_ENDED then
      CCLuaLog('onTOUCH ended')
    elseif event == TOUCH_EVENT_CANCELED then
      CCLuaLog('onTOUCH cancled')
    end
  end
  )

  local mySlider = UIHelper:seekWidgetByTag(myUIScene, 105)
  mySlider:addEventListenerSlider(                                  --event===0，began,ended,moved,都会产生回调
  --mySlider:addTouchEventListener(                                 --有began跟ended事件，但没有moved，移动的时候不回调
    --[[
    function(...)
      local arg = {...}
      printf('addEventListenerSlider arg number = %d', #arg)
      printf('type of arg[1]=%s,arg[2]=%s',type(arg[1]),type(arg[2]))
    end
    --]]
    function(tager,event) 
    --CCLuaLog('onTOUCH')
    --[[
      if event == TOUCH_EVENT_BEGAN then 
        CCLuaLog(type(touch))
        CCLuaLog(type(event))
        CCLuaLog('onTOUCH began')
        printf('tager name = %s',tager:getName())
      elseif event == TOUCH_EVENT_MOVED then
        CCLuaLog('onTOUCH moved')
      elseif event == TOUCH_EVENT_ENDED then
        CCLuaLog('onTOUCH ended')
      elseif event == TOUCH_EVENT_CANCELED then
        CCLuaLog('onTOUCH cancled')
      end
      --]]
      printf('event = %d',event)
      return true
    end
  )

  local myCheckBox = UIHelper:seekWidgetByTag(myUIScene, 107)
  myCheckBox:addEventListenerCheckBox(                                --event实际为box的状态，0选中，1取消选中状态，只有在触点放开时触发
  --myCheckBox:addTouchEventListener(                                 --有效果同UIButton
    --[[
    function(...)
      local arg = {...}
      printf('addEventListenerSlider arg number = %d', #arg)
      printf('type of arg[1]=%s,arg[2]=%s',type(arg[1]),type(arg[2]))
    end
    --]]
    ---[[
    function(tager,event) 
      printf('tager name = %s', tager:getName())
      printf('event = %d',event)
    end
    --]]
  )

  local myPage = UIHelper:seekWidgetByTag(myUIScene, 106)
  printf('myPageView name = %s', myPage:getName())
  printf('myPageView CurPageIndex = %d',myPage:getCurPageIndex())
  myPage:scrollToPage(1)
  printf('myPageView CurPageIndex = %d',myPage:getCurPageIndex())
  myPage:addEventListenerPageView(                                --event===0，began,ended,会产生回调，初始化后会自动触发第一次
  --myPage:addTouchEventListener(                                 --无法触发回调
      --[[
      function(...)
        local arg = {...}
        printf('addEventListenerSlider arg number = %d', #arg)
        printf('type of arg[1]=%s,arg[2]=%s',type(arg[1]),type(arg[2]))
      end
      --]]
      ---[[
      function(tager,event) 
        printf('tager name = %s', tager:getName())
        printf('event = %d',event)
      end
      --]]
  )
  local page1 = myPage:getChildByTag(107)
  if not page1 then CCLuaLog('...Not have page1...')
  else
    CCLuaLog('...have page1...')
    printf('page1 name = %s', page1:getName())
  end
  local children = page1:getChildByTag(1071)
  printf('children name = %s', children:getName())

  local image1081 = UIHelper:seekWidgetByTag(myUIScene, 1081)
  printf('image1081 name = %s', image1081:getName())

  local myScrollView = UIHelper:seekWidgetByTag(myUIScene, 109)
  printf('myScrollView name = %s', myScrollView:getName())
  printf('myScrollView isBounceEnabled = %s', tostring(myScrollView:isBounceEnabled()))
  myScrollView:setBounceEnabled(false)                                                                  --取消回弹效果
  printf('myScrollView isInertiaScrollEnabled = %s', tostring(myScrollView:isInertiaScrollEnabled()))
  --myScrollView:setInertiaScrollEnabled(false)                                                         -- ???????
  local size = myScrollView:getInnerContainerSize()
  printf('myScrollView InnerContainerSiz w=%d,h=%d', size.width,size.height)
  myScrollView:setInnerContainerSize(CCSize(160,600))
  myScrollView:addTouchEventListener(
    function(tager,event) 
      printf('tager name = %s', tager:getName())
      printf('event = %d',event)
    end
  )

end

local index = 0
local percent = 0
function onClicked(tag)
  CCLuaLog('... onClicked ...')
  --local scene = display:getRunningScene():getChildByTag(TAG_MYSCENE):getRootWidget()
  --local scene = display:getRunningScene():getChildByTag(TAG_MYSCENE):getWidgetByTag(100)
  local scene = display:getRunningScene():getChildByTag(TAG_MYSCENE):getWidgetByName('rootNode')
  local slider = UIHelper:seekWidgetByTag(scene, 105)
  printf('slider percent : %g',slider:getPercent())

  local checkBox = UIHelper:seekWidgetByTag(scene, 107)
  printf('checkBox SelectedState : %s',tostring(checkBox:getSelectedState()))

  local myPage = UIHelper:seekWidgetByTag(scene, 106)
  myPage:scrollToPage(index)
  if index == 0 then index = 1 else index = 0 end

  local scroll = UIHelper:seekWidgetByTag(scene, 109)
  scroll:scrollToPercentVertical(percent, 0.4, true)                                    --第二个参数为时间，第三个参数，滞缓效果？
  --scroll:jumpToPercentVertical(percent)                                               --立即跳到percent，不带scroll滑动效果
  percent = percent + 10
  if percent > 100 then percent = 0 end


  return false
end


function onTouch(event)
  if event.name == 'began' then
    CCLuaLog('... onTouch ...')
  elseif event.name == 'moved' then


  end

 
  return true
 
end


function GameScene:onEnter()
	CCLuaLog('GameScene:onEnter')																				--启用定时器监听
end


function GameScene:enterScene(sceneName, args, transitionType, time, more)
	 print('GameScene:enterScene..................')
end

function GameScene:onEnterFrame(dt)
  
end

game = {}

function game.startup()
    CCFileUtils:sharedFileUtils():addSearchPath("res/")
    print('startup..................')
    game.enterMainScene()
end

function game.exit()
    CCDirector:sharedDirector():endToLua()
    print('exit..........')
end

function game.enterMainScene()
	print('GameScene.new',type(GameScene.new))
    display.replaceScene(GameScene.new(), "fade", 0.6, display.COLOR_WHITE)
end
return game